<?php session_start();

require_once('./classes/libs/Smarty.class.php');
require_once('./classes/Config.class.php');
require_once('./classes/Content.class.php');
require_once('./classes/ContentMetaData.class.php');
require_once('./classes/plugins_tinymce/JsHttpRequest.php');
require_once('./classes/plugins_tinymce/tinyimages.php');

$q = isset($_GET["q"]) ? $_GET["q"] : "";
$host = getHost();	
$conf = new Config();
$conf->parse_ini_file("./configs/config.ini");
if ($conf->parse_ini_file("./configs/".$host.".ini")){
	//On continu
}
else if(getDomain() != "www"){
	header('Status: 301 Moved Permanently', false, 301);   
	header('Location: http://www.'.$host);   
	exit();  
}
else{
	die($host . " inconnu");
}

$smarty = new Smarty();
$smarty->addPluginsDir('./classes/plugins/');
$smarty->addTemplateDir('./private/'.$host.'/templates/'.$conf->getTemplateDir().'/');
$smarty->addTemplateDir('./private/common/templates/'.$conf->getTemplateDir().'/');

$smarty->addConfigDir('./private/'.$host.'/templates/'.$conf->getTemplateDir().'/');
$smarty->addConfigDir('./private/common/templates/'.$conf->getTemplateDir().'/');

$smarty->compile_dir = './tmp/templates_c/'.$host .'/';
$smarty->cache_dir = './tmp/cache/'.$host.'/';

//$smarty->force_compile = true;
//$smarty->debugging = true;
$smarty->caching = false;
$smarty->cache_lifetime = 120;



$metadata = new ContentMetaData('./private/'.$host.'/content/', 'content.metadata');

$smarty->assign("contextpath", 'resources/'.$conf->getTemplateDir() );
$smarty->assign("metadata", $metadata);
$smarty->assign("conf", $conf);

$conf->invokeInitFunction($smarty);
$conf->invokeMapping($q, $_SESSION,$smarty);
$conf->invokeEndFunction($smarty);


function getSubDomain(){ 
	return substr_count($_SERVER['HTTP_HOST'], '.') >= 1 ? 
		substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], '.')) : ''; 
}
function getDomain(){ 
	return substr_count($_SERVER['HTTP_HOST'], '.') >= 1 ? 
		substr($_SERVER['HTTP_HOST'], strpos($_SERVER['HTTP_HOST'], '.') + 1) : $_SERVER['HTTP_HOST']; 
}

function getHost(){ 
	return $_SERVER['HTTP_HOST']; 
}

?>
